/*
Name: Kabir Kang
Date: 7/27/14
Summary: Randomized test for adventurer card
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>

#include "dominion.h"
#include "dominion_helpers.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int const NUM_TESTS = 10000;

void zzzadventurercardplay(int drawntreasure, int currentPlayer, int cardDrawn,
	int temphand[MAX_HAND], int z, struct gameState* state); //needed to declare function for some reason. without it i was getting errors, which i am still trying to understand

int adventurerTest()
{
	int i, j, l;
  struct gameState g;
  struct gameState * G = &g;

  memset(G, 0, sizeof(struct gameState));
        
  //Game initialization
#if (NOISY_TEST==1)
  printf("Initializing game...\n");
#endif
  int k[10] = {adventurer, gardens, embargo, village, minion, mine, cutpurse, 
	       sea_hag, tribute, smithy};

  int numPlayers = 2 + rand()%3;  //we need at least 2 players
  initializeGame(numPlayers, k, time(NULL), G);

  int cardDrawn = rand()%26;

#if (NOISY_TEST==1)
  printf("Randomizing game...\n");
#endif
  //Game randomization
  int handCount = rand()%MAX_HAND;
  int deckCount = rand() % (MAX_DECK - handCount); //the deck is randomly generated and is less than or equal to 500 minus hand
  int discardCount = MAX_DECK - deckCount; //discard pile is 500 minus the deck count
  int currentPlayer = rand()%numPlayers;//current player will be random too

  int temphand[MAX_HAND];
        
  G->deckCount[currentPlayer] = deckCount;
  G->discardCount[currentPlayer] = discardCount;
  G->handCount[currentPlayer] = handCount;
        
  for (i = 0; i < deckCount; i++)
  {
	  G->deck[currentPlayer][i] = rand() % 26;
  }
  for (j = 0; j < discardCount; j++)
  {
	  G->discard[currentPlayer][j] = rand() % 26;
  }
  for (l = 0; l < handCount; l++)
  {
	  G->hand[currentPlayer][l] = rand() % 26;
  }
  
#if (NOISY_TEST==1)
  printf("Calling adventurer...\n");
#endif  
  zzzadventurercardplay(0, currentPlayer, cardDrawn, temphand, 0, G); //z and drawntreasure are 0
        
  if (
	  (G->handCount[currentPlayer] == handCount + 2) && (G->hand[currentPlayer][G->handCount[currentPlayer] - 1] == copper || silver || gold) && (G->hand[currentPlayer][G->handCount[currentPlayer] - 2] == copper || silver || gold))
  {
#if (NOISY_TEST==1)
	  printf("Adventurer: PASS when 2 treasures drawn \n");
#endif
  }
  else {
#if (NOISY_TEST==1)
	  printf("Adventurer: FAIL when 2 treasures drawn\n");
#endif
  
    return -1;
  }
        
  return 0;
}

int main()
{
	int i;
  srand(time(NULL));
        
  for (i = 0; i < NUM_TESTS; i++)
  {
	  if (adventurerTest() < 0)
	  {
		  break;
	  }
  }
        
  return 0;
}
